
import hashlib
from db.entity import BaseEntity
# 创建表操作类

class Department(BaseEntity):
    """
    表格department对应的实体类
    """
    def __init__(self):
        super(Department, self).__init__('department')
    
    def get_name(self, id):
        try:
            return self.get(['name'],[['id', id]]).as_dict()[0]['name']
        except Exception as e:
            return None
    

class User(BaseEntity):
    """
    表格user对应的实体类
    """
    def __init__(self):
        super(User, self).__init__('user')
    
    def check_pwd(self, number, pwd):
        p = self.get([], [['number', number], ]).as_dict()
        if len(p) != 1:
            return False
        _pwd = p[0].pop('password')
        if type(_pwd) is not str:
            _pwd = str(_pwd)
        tpwd = hashlib.md5()
        tpwd.update(bytes(_pwd, encoding='utf-8'))
        # print(tpwd.hexdigest())
        if len(p) > 0 and tpwd.hexdigest() == pwd:
            p[0].update({'dep':  (p[0]['dep'], self.get_department(number))})
            return p[0]
        else:
            return False

    def get_department(self, name):
        try:
            p = self.get(['dep', ], [['number', name], ]).as_dict()
            if p[0]['dep']:
                dep = Department()
                return dep.get_name(p[0]['dep'])
        except Exception as e:
            print(e)
            return
    

class FormattedMessage(BaseEntity):
    """
    表格formatted_message对应的实体类
    """
    def __init__(self):
        super(FormattedMessage, self).__init__('formatted_message')
    

class FormattedMessageCategory(BaseEntity):
    """
    表格formatted_message_category对应的实体类
    """
    def __init__(self):
        super(FormattedMessageCategory, self).__init__('formatted_message_category')
    

class FormattedText(BaseEntity):
    """
    表格formatted_text对应的实体类
    """
    def __init__(self):
        super(FormattedText, self).__init__('formatted_text')
    

